package com.cn.jinl.controller;

import com.cn.jinl.api.IRoleService;
import com.cn.jinl.domain.Role;
import com.cn.jinl.utils.CommonUtil;
import com.cn.jinl.utils.JsonResponse;
import com.cn.jinl.utils.QueryForm;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/role")
public class RoleController {

    @Autowired
    private IRoleService roleService;

    @ResponseBody
    @RequestMapping("/getRoleList/{frontToken}")
    public JsonResponse<List<Role>> getRoleList(QueryForm queryForm,@PathVariable(value = "frontToken",required = true) String frontToken) throws Exception {
        JsonResponse<List<Role>> response = new JsonResponse<List<Role>>();
        List<Role> role = roleService.getRoleList(queryForm);
        response.setResult(role);
        response.setSuccess(true);
        return response;
    }

    @ResponseBody
    @RequestMapping("/addRoleDetail/{frontToken}")
    public JsonResponse<String> addRoleDetail(Role role,@PathVariable(value = "frontToken",required = true) String frontToken) throws Exception {
        JsonResponse<String> response = new JsonResponse<String>();
        String result = roleService.addRoleDetail(role);
        return CommonUtil.getResponseData(response,result);
    }

    @ResponseBody
    @RequestMapping("/updateRoleDetail/{frontToken}")
    public JsonResponse<String> updateRoleDetail(Role role,@PathVariable(value = "frontToken",required = true) String frontToken) throws Exception {
        JsonResponse<String> response = new JsonResponse<String>();
        String result = roleService.updateRoleDetail(role);
        return CommonUtil.getResponseData(response,result);
    }

    @ResponseBody
    @RequestMapping("/deleteRoleDetail/{frontToken}")
    public JsonResponse<String> deleteRoleDetail(@RequestBody QueryForm queryForm, @PathVariable(value = "frontToken",required = true) String frontToken) throws Exception {
        JsonResponse<String> response = new JsonResponse<String>();
        String result = roleService.deleteRoleDetail(queryForm);
        return CommonUtil.getResponseData(response,result);
    }

    @ResponseBody
    @RequestMapping("/queryRolePermission")
    public JsonResponse<List<String>> queryRolePermission(String roleId) throws Exception {
        JsonResponse<List<String>> response = new JsonResponse<List<String>>();
        List<String> role = roleService.queryRolePermission(roleId);
        response.setResult(role);
        response.setSuccess(true);
        return response;
    }

}
